package com.ternnetwork.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.core.dao.RescRoleDao;
import com.ternnetwork.core.dao.ResourcesDao;
import com.ternnetwork.core.dao.RoleDao;
import com.ternnetwork.core.entity.RescRole;
import com.ternnetwork.core.entity.RescRoleId;
import com.ternnetwork.core.entity.Resources;
import com.ternnetwork.core.entity.Role;
import com.ternnetwork.core.enums.ResourcesType;
import com.ternnetwork.core.enums.RoleType;
import com.ternnetwork.core.service.RescRoleService;
import com.ternnetwork.core.ui.CheckBoxResourcesZtree;








@Service("rescRoleService")
public class RescRoleServiceImpl implements RescRoleService {

	@Resource
	private RescRoleDao rescRoleDao;
	
	@Resource
	private ResourcesDao resourcesDao;
	
	@Resource
	private RoleDao roleDao;
	
	
	
	/**
	 * 按角色ID来查找所有资源并将已关联角色的资源标注为checked=true
	 * @param roleId
	 * @param roleType
	 * @param resourcesType
	 * @return
	 */
	@Override
	public List<CheckBoxResourcesZtree> getTreeJSON(long roleId,RoleType roleType,ResourcesType resourcesType){
		
		List<Resources> allList=resourcesDao.findAll("from Resources t where t.roleType=?1 and t.type=?2 order by t.seqNum",roleType,resourcesType);

		List<Resources> roleReslist=resourcesDao.findAll("from Resources t where t.roleType=?1 and t.type=?2 and t.id in (select resources.id from RescRole where role.id=?3) order by t.seqNum",roleType,resourcesType,roleId);
		
		List<CheckBoxResourcesZtree> retVal = convertToZtreeList(allList,roleReslist);
		
		return retVal;
		
	}
	
	
	private List<CheckBoxResourcesZtree> convertToZtreeList(List<Resources> allList,List<Resources> roleReslist) {
		List<CheckBoxResourcesZtree> retVal=new ArrayList<CheckBoxResourcesZtree>();
		for(Resources t:allList){
			CheckBoxResourcesZtree tree=new CheckBoxResourcesZtree();
			tree.setId(t.getId());
			tree.setpId(t.getParentId());
			tree.setName(t.getNameZh());
			tree.setNameZh(t.getNameZh());
			tree.setNameEn(t.getNameEn());
			tree.setUrl(t.getUrl());
			tree.setRoleType(t.getRoleType());
			tree.setType(t.getType());
			tree.setSeqNum(t.getSeqNum());
			String[] roleIds=t.getRoleIds().equals("")?new String[]{}:t.getRoleIds().split(",");
			tree.setRoleIds(roleIds);
			tree.setChecked(checkExist(t.getId(),roleReslist));
			retVal.add(tree);
		}
		return retVal;
	}
	@Override
	public BaseResponse idoSave(long roleId,RoleType roleType,ResourcesType resourcesType,String resourcesIds){
		
		BaseResponse res=new BaseResponse();
		
		String[] resourcesIdsArray=resourcesIds.split(",");
		
		doDelete(roleId, roleType, resourcesType, resourcesIdsArray);//删除未选中的
		
		doAdd(roleId, resourcesIdsArray);//添加已选中的
		
		res.setMessage("操作成功！");
		
		res.setStatus(1);
		
		return res;
	}


	private void doAdd(long roleId, String[] resourcesIdsArray) {
		for(String resourcesIdStr:resourcesIdsArray){
			if(!StringUtils.isEmpty(resourcesIdStr)){
				Long  resourcesId=Long.parseLong(resourcesIdStr);
				RescRoleId id=new RescRoleId();
				id.setResourcesId(resourcesId);
				id.setRoleId(roleId);
				
				if(rescRoleDao.findById(RescRole.class, id)==null){//如果不存在则添加
					RescRole rescRole=new RescRole();
					rescRole.setId(id);
					rescRole.setRole(roleDao.findById(Role.class, roleId));
					rescRole.setResources(resourcesDao.findById(Resources.class, resourcesId));
					rescRoleDao.persist(rescRole);
				}
			}
		}
	}


	private void doDelete(long roleId, RoleType roleType, ResourcesType resourcesType, String[] resourcesIdsArray) {
		List<RescRole> allList=rescRoleDao.findAll("from RescRole t where t.role.id=?1 and t.resources.type=?2 and t.resources.roleType=?3",roleId,resourcesType,roleType);
		for(RescRole t:allList){
			if(checkExist(t.getResources().getId(), resourcesIdsArray)==false){//如果已有的资源不在resourcesIds中则将其删除
				RescRole deleteRescRole=rescRoleDao.getReferenceById(RescRole.class, t.getId());
				if(deleteRescRole!=null){
					rescRoleDao.delete(deleteRescRole);
				}
			}
		}
	}
	
	private Boolean checkExist(long resourcesId,String[] resourcesIdsArray){
		for(String resourcesIdStr:resourcesIdsArray){
			if(Long.parseLong(resourcesIdStr)==resourcesId){
				return true;
			}
		}
		return false;
	}
	
	private Boolean checkExist(long resourcesId,List<Resources> roleReslist){
		for(Resources t:roleReslist){
			if(t.getId()==resourcesId){
				return true;
			}
		}
		return false;
	}


	public List<RescRole> queryByResourcesId(long resourcesId){
		return rescRoleDao.findAll("from RescRole t where t.id.resourcesId=?1",resourcesId);
	}
	
	@Override
	public void idoUpdateBatch(long resourcesId,String deleteRoleIds,String addRoleIds){
		doDeleteBatch(resourcesId, deleteRoleIds);
		doAddBatch(resourcesId, addRoleIds);
	}
	@Override
	public void doDeleteBatch(long resourcesId,String deleteRoleIds){
		String[] deleteRoleId=deleteRoleIds.equals("")?new String[]{}:deleteRoleIds.split(",");
		List<Resources> list=resourcesDao.findAllChild(new ArrayList<Resources>(),resourcesId);
		for(String roleId:deleteRoleId){
				for(Resources t:list){
				RescRoleId id=new RescRoleId();
				id.setResourcesId(t.getId());
				id.setRoleId(Long.parseLong(roleId));
				rescRoleDao.bulkUpdate("delete from RescRole t where t.id=?1",id);
			}
		}
	}
	@Override
	public void doAddBatch(long resourcesId,String addRoleIds){
		String[] addRoleId=addRoleIds.equals("")?new String[]{}:addRoleIds.split(",");
    	List<Resources> list=resourcesDao.findAllParent(new ArrayList<Resources>(),resourcesId);
		for(String roleId:addRoleId){
				for(Resources t:list){
				RescRoleId id=new RescRoleId();
				id.setResourcesId(t.getId());
				id.setRoleId(Long.parseLong(roleId));
				RescRole rescRole=rescRoleDao.findById(RescRole.class,id);
				if(rescRole==null){
					rescRole=new RescRole();
					rescRole.setId(id);
					rescRoleDao.persist(rescRole);
				}
			}
		}
   	}
	
	
}
